﻿using System.Collections;

namespace BullsEyeGame
{
    internal class GameData
    {
        private ArrayList m_GuessResultArrayList = null;
        private Guess m_TargetGuess = null;
        private int m_MaxNumberOfGuessesChosen; 

        private GameData(int i_MaxNumberOfGuessesChosen)
        {
            m_MaxNumberOfGuessesChosen = i_MaxNumberOfGuessesChosen;
            m_GuessResultArrayList = new ArrayList(i_MaxNumberOfGuessesChosen);
        	generateTargetGuess();
        }

        public static GameData CreateGameData(int i_MaxNumberOfGuessesChosen)
        {
            bool validGuessChosen = GameLogics.k_MinNumberOfGuesses <= i_MaxNumberOfGuessesChosen;

            validGuessChosen = validGuessChosen && (i_MaxNumberOfGuessesChosen <= GameLogics.k_MaxNumberOfGuesses);

            return validGuessChosen ? new GameData(i_MaxNumberOfGuessesChosen) : null;
        }

        public ArrayList GuessesResults
        {
            get { return m_GuessResultArrayList; }
        }

        public Guess TagetGuess
        {
            get { return m_TargetGuess; }
        }

        public GuessResult AddNewGuess(Guess i_NewGuess)
        {
            GuessResult newGuessResult = GuessResult.CreateGuessResult(i_NewGuess);

            if (newGuessResult != null)
            {
                m_GuessResultArrayList.Add(newGuessResult);
            }

            return newGuessResult;
        }

        private void generateTargetGuess()
        {
            System.Random randomNumber = new System.Random();
            char[] targetCharArray = new char[Guess.k_GuessLength];
            int currentGeneratedNumber;

            for (int i = 0; i < targetCharArray.Length; ++i)
            {
                currentGeneratedNumber = randomNumber.Next(Guess.k_ValidChars.Length - 1);
                targetCharArray[i] = Guess.GetValidCharsAt(currentGeneratedNumber);
            }

            m_TargetGuess = Guess.CreateGuess(new string(targetCharArray));
        }

        public int MaxNumberOfCuessesChosen
        {
            get { return m_MaxNumberOfGuessesChosen; }
            set { m_MaxNumberOfGuessesChosen = value; }
        }
    }
}